package com.woniuxy.oa.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.oa.entity.Approval;
import com.woniuxy.oa.entity.Employee_Holiday;
import com.woniuxy.oa.entity.Holiday;
import com.woniuxy.oa.entity.HolidayType;
import com.woniuxy.oa.exception.ApprovalException;
import com.woniuxy.oa.exception.HolidayException;
import com.woniuxy.oa.mapper.HolidayMapper;
import com.woniuxy.oa.service.HolidayService;

@Service
public class HolidayServiceImpl implements HolidayService{

	@Autowired
	HolidayMapper holidayMapper;
	
	public void setHolidayMapper(HolidayMapper holidayMapper) {
		this.holidayMapper = holidayMapper;
	}
	@Override
	public Integer addHolidayGetId(Holiday holiday) {
		try {
			holidayMapper.insertHolidayGetId(holiday);
			Integer getId = holiday.getH_id();
			if(getId<=0) {
				throw new Exception("插入失败");
			}
			return getId;
		} catch (Exception e) {
			e.printStackTrace();
			throw new HolidayException(e);
		}
	}
	@Override
	public Integer addEmp_holiday(Employee_Holiday employee_Holiday) {
		try {
			holidayMapper.insertEmployee_HolidayGetId(employee_Holiday);
			Integer eh_id = employee_Holiday.getEh_id();
			if(eh_id<=0) {
				throw new HolidayException("插入addEmp_holiday失败");
			}
			return eh_id;
		} catch (Exception e) {
				e.printStackTrace();
				throw new HolidayException(e);
		}
	
	}
	@Override
	public List<HolidayType> getHolidayType() {
		List<HolidayType> hTypes = null;
		try {
			hTypes = holidayMapper.selectHolidayType();
			if(hTypes!=null) {
				return hTypes;
			}
			throw new HolidayException("查找假期类型失败");
		} catch (Exception e) {
			e.printStackTrace();
			throw new HolidayException(e);
		}
		
	}
	@Override
	public Integer getIdByAid(Integer a_id) {
		try {
			Integer e_id = holidayMapper.selectE_idByA_id(a_id);
			if(e_id>0) {
				return e_id;
			}
			throw new ApprovalException("没有根据aid"+a_id+"找到发起人id");
		} catch (Exception e) {
			throw new ApprovalException(e);
		}
	}
	@Override
	//根据审批号将假期申请的flag改为1
	public void deleteHolidayByAid(Integer a_id) {
		try {
			holidayMapper.updateHolidayFlagByAid(a_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new HolidayException(e);
		}
		
	}
	@Override
	public Holiday selectHolidayByHid(Integer h_id) {
		try {
			Holiday holiday = holidayMapper.selectHolidayByHid(h_id);
			return holiday ; 
		} catch (Exception e) {
			e.printStackTrace();
			throw new HolidayException(e);
		}
		
	}
	@Override
	public Approval selectApprovalByAid(Integer a_id) {
		try {
			Approval selectApprovalByAid = holidayMapper.selectApprovalByAid(a_id);
			return selectApprovalByAid ; 
		} catch (Exception e) {
			e.printStackTrace();
			throw new HolidayException(e);
		}
	}
	
	//修改请假信息
	@Override
	public void updateHoliday(Holiday holiday,Integer a_id) {
		try {
			holidayMapper.updateHoliday(holiday,a_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new HolidayException(e);
		}
	}
	//修改审批关系
	@Override
	public void updateApprovalAeid(Integer a_eid, Integer a_id) {
		try {
			holidayMapper.updateApprovalAeid(a_eid, a_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new HolidayException(e);
		}
		
	}
	
	//根据员工编号找到请假审批中已通过的，看看当前时间是否在其区间内

	@Override
	public Holiday getExaminedRecord(int e_id, String now) {
		try {
			return holidayMapper.selectHolidayByEidAndNowTime(e_id,now);
		} catch (Exception e) {
			e.printStackTrace();
			throw new HolidayException(e);
		}
	}
	
}
